"""
Assuming that only URLs with http:// and/or www. in fornt of them
are valid URL and also the according to the main grammar (not considering the RFC)
,i.e., schema://<Resource Locator>, the resource locator shoud have a alphanum
property
"""
import urlparse
import re
from main_interface import log_init


def test_body(url):
    pieces = urlparse.urlparse(url)
    if pieces.scheme == "http" or pieces.scheme == "https":
        if re.match('^[a-zA-Z0-9_.]+$',pieces.netloc):
            return True
        else:
            return False


def is_valid_url(url):
    regex = re.compile(
        r'^https?://'  # http:// or https://
        r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|'  # domain...
        r'localhost|'  # localhost...
        r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
        r'(?::\d+)?'  # optional port
        r'(?:/?|[/?]\S+)$', re.IGNORECASE)
    if test_body(url) == False:
        return None
    return url is not None and regex.search(url)